package com.james.controller;

import com.james.domain.User;
import com.james.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: UserController
 * @Author: JamesWang
 * @Date: 2021/4/22 21:30
 * @Description:
 * @Version:1.0
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 用户积分查询
     * @return
     */
    @GetMapping("getScore")
    public ResponseEntity<Map<String,Object>> getScore() {

        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        User user = userService.getById(userId);
        Map<String, Object> map = new HashMap<>(8);
        map.put("code",200);
        map.put("msg","ok");
        map.put("data",user.getScore());
        return ResponseEntity.ok(map);
    }

    @PostMapping("userLogOut")
    public ResponseEntity<Map<String,Object>> userLogOut(@RequestHeader("Authorization") String authorization) {

        String JWTToken = authorization.replaceAll("bearer ", "");
        redisTemplate.delete("oauth:jwt:" + JWTToken);

        Map<String, Object> map = new HashMap<>(8);
        map.put("code",200);
        map.put("msg","登出成功");
        return ResponseEntity.ok(map);
    }

}
